//
//  LoginViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/8/26.
//  Copyright © 2016年 KX. All rights reserved.
#import "RegisterViewController.h"
#import "Masonry.h"
#import "LoginViewController.h"
#import "FreeTravelMacro.h"
#import "FreeTravelNetwork.h"
#import "PhoneLoginViewController.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationItem.title = @"登录";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    
    self.view.backgroundColor = [UIColor colorWithRed:0.11 green:0.68 blue:0.45 alpha:1.00];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ghffghfhgfg"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    

    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(goRegister:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:loginButton];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton setTitleColor:COLORFORFREETRAVEL forState:UIControlStateNormal];
    loginButton.backgroundColor = [UIColor whiteColor];
    loginButton.layer.cornerRadius = 5;
    loginButton.layer.masksToBounds = YES;
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(44);
        make.center.mas_equalTo(self.view);
    }];
    [loginButton addTarget:self action:@selector(postLogin) forControlEvents:UIControlEventTouchUpInside];
    
    self.userTextField = [[UITextField alloc]init];
    [self.view addSubview:self.userTextField];

    self.userTextField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"输入邮箱/用户名" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
    [self.userTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).multipliedBy(0.4);
        make.height.mas_equalTo(self.view).multipliedBy(0.08);
        make.width.mas_equalTo(self.view).multipliedBy(0.8);
    }];
    
    UIView *firstWhiteLine = [[UIView alloc]init];
    firstWhiteLine.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:firstWhiteLine];
    [firstWhiteLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.equalTo(self.userTextField.mas_bottom);
        make.height.mas_equalTo(2);
        make.width.mas_equalTo(self.view).multipliedBy(0.8);
    }];
    
    self.passwordTextField = [[UITextField alloc]init];
    self.passwordTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordTextField];
    self.passwordTextField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"输入密码" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerX.mas_equalTo(self.userTextField);
        make.top.equalTo(firstWhiteLine.mas_bottom);
    }];
    
    UIView *secondWhiteLine = [[UIView alloc]init];
    [self.view addSubview:secondWhiteLine];
    secondWhiteLine.backgroundColor = [UIColor whiteColor];
    [secondWhiteLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerX.equalTo(firstWhiteLine);
        make.top.equalTo(self.passwordTextField.mas_bottom);
    }];
    
    UIButton *phoneLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:phoneLoginButton];
    [phoneLoginButton setTitle:@"手机号登陆" forState:UIControlStateNormal];
    [phoneLoginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [phoneLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.width.height.equalTo(loginButton);
        make.top.equalTo(loginButton.mas_bottom);
    }];
    [phoneLoginButton addTarget:self action:@selector(goPhoneLogin:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)goPhoneLogin:(UIButton *)button{
    PhoneLoginViewController *vc = [[PhoneLoginViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)postLogin{
    NSLog(@"%@   %@", self.userTextField.text, self.passwordTextField.text);
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    if ([emailTest evaluateWithObject:self.userTextField.text] && (self.passwordTextField.text != nil)) {
        [FreeTravelNetwork POST:@"http://open.qyer.com/qyer/user/login" andBody:[NSString stringWithFormat:@"account_s=d2a21bf8d23ac0b65bbe4211f703e772&account_type=1&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&grant_type=password&page=1&password=%@&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.4&username=%@&v=1",self.passwordTextField.text,self.userTextField.text] andBodyStyle:ZWYBodyString andHeader:@{@"Host":@"open.qyer.com",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @"Content-Type": @"application/x-www-form-urlencoded",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @"Accept":@" */*",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @"Connection": @"keep-alive",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @"Proxy-Connection": @"keep-alive",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @"Cookie": @"__utma=253397513.2045201602.1472089545.1472193699.1472211994.9; __utmz=253397513.1472089545.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _guid=84639271-4ee1-e7cc-4fa4-85f96ce0f519; plan_token=49a54885675d4aa2755c0f6f6f596a9e; yqf_cps=source%3Demar%26channel%3Dcps%26cid%3D18526%26wi%3DNTEzNzAzfDAwMTBiNDU1NDU5OTYxODY0OTAz",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @"User-Agent":@" QYER/7.0.1 (iPhone; iOS 9.3.4; Scale/2.00) Paros/3.2.13",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @"Accept-Language":@" zh-Hans-CN;q=1, nl-CN;q=0.9, en-CN;q=0.8",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                @"Content-Length":@"348"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    NSUserDefaults *userDeafut = [NSUserDefaults standardUserDefaults];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    NSInteger access_token = (NSInteger)[[result objectForKey:@"data"] objectForKey:@"access_token"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    NSString *avatar = [[result objectForKey:@"data"] objectForKey:@"avatar"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    NSString *cover = [[result objectForKey:@"data"] objectForKey:@"cover"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    NSString *user_id = [[result objectForKey:@"user_id"] objectForKey:@"cover"];NSString *username = [[result objectForKey:@"data"] objectForKey:@"username"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    NSString *title = [[result objectForKey:@"data"] objectForKey:@"title"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    [userDeafut setInteger:access_token forKey:@"access_token"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    [userDeafut setObject:avatar forKey:@"avatar"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    [userDeafut setObject:cover forKey:@"cover"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    [userDeafut setObject:user_id forKey:@"user_id"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    [userDeafut setObject:username forKey:@"username"];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    [userDeafut setObject:title forKey:@"title"];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    [userDeafut setBool:YES forKey:@"status"];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        self.passResult(result);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    [self dismissViewControllerAnimated:YES completion:^{
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }   ];                                                                        } andFailureBlock:^(NSError *error) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }];
    }else{
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"请输入正确的邮箱号和密码" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *certain = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertVC addAction:alertAction];
        [alertVC addAction:certain];
        [self presentViewController:alertVC animated:YES completion:^{
            
        }];

    }
    
}
- (void)goBack:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)goRegister:(UIButton *)button{
    RegisterViewController *regist = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:regist animated:YES];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
